package com.application.lucky.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.application.lucky.Lucky;
import com.application.lucky.R;
import com.application.lucky.bean.eventbean.ForceUpgradeEvent;
import com.application.lucky.manager.SocketListenerManager;
import com.application.lucky.presenter.LoginPresenter;
import com.application.lucky.view.LoginView;
import com.application.lucky.widget.dialog.SignleButtonDialog;
import com.application.mylibrary.base.mvp.BaseMvpActivity;
import com.application.mylibrary.utils.EventBusUtils;
import com.application.mylibrary.utils.ToastUtils;
import com.qunar.im.ui.util.StatusBarUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseMvpActivity<LoginView, LoginPresenter> implements LoginView, View.OnClickListener {

    private EditText etUsername;
    private EditText etPassword;
    private Button btLogin;
    private TextView tvRegister;
    private TextView tvForget;
    private boolean isout;

    @Override
    public LoginView createView() {
        return this;
    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SocketListenerManager.getInstance().clear();
        EventBusUtils.register(LoginActivity.this);
        initView();
        initlistener();
        clearOldData();
    }


    private void initlistener() {
        btLogin.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
        tvForget.setOnClickListener(this);
    }

    private void initView() {
        if (isout) {
            ToastUtils.show("退出成功");
        }
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        btLogin = findViewById(R.id.bt_login);
        tvRegister = findViewById(R.id.tv_register);
        tvForget = findViewById(R.id.tv_forget);

    }

    @Override
    public void beforeInit() {
        isout = getIntent().getBooleanExtra("isout", false);
    }

    @Override
    public int getLayoutId() {
        StatusBarUtil.setTranslucentForImageView(this, null);
        return R.layout.activity_login;
    }

    @Override
    public String getUsername() {
        return etUsername.getText().toString().trim();
    }

    @Override
    public String getPassword() {
        return etPassword.getText().toString().trim();
    }

    @Override
    public void Success() {
        startActivityAnim(new Intent(this, MainActivity.class));
        finishAnim();
    }


    @Override
    public void loginFail() {

    }

    @Override
    public void showToast(String message) {
        ToastUtils.show(message);
    }

    @Override
    public void error(int code) {
        if (code == 10003) {
            showToast("用户名密码错误");
        } else {
            showToast("请求失败");
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_login:
                if (getPresenter() != null) {
                    getPresenter().login();
                }
                break;
            case R.id.tv_register:
                startActivityAnim(new Intent(this, RegisterActivity.class));
                finish();

                break;

            case R.id.tv_forget:
                startActivityAnim(new Intent(this, ForgetPasswordActivity.class));
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusUtils.unregister(LoginActivity.this);
        if (logoutDialog != null && logoutDialog.isShowing()) {
            logoutDialog.dismiss();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEventUpgrade(ForceUpgradeEvent event) {
        if (logoutDialog != null && logoutDialog.isShowing()) {
            return;
        }
        logoutDialog = null;
        showLogoutDialog();
    }

    private SignleButtonDialog logoutDialog;

    private void showLogoutDialog() {
        logoutDialog = new SignleButtonDialog.Build(LoginActivity.this)
                .disMiss(true)
                .setMessage("当前版本过低，更新到后才能使用？")
                .setRightButton("更新", new SignleButtonDialog.BaseDialogClickListener() {
                    @Override
                    public void click() {
                        logoutDialog.dismiss();
                    }
                }).create();
        logoutDialog.setCanceledOnTouchOutside(false);
        logoutDialog.show();
    }

    private void clearOldData() {
        Observable observable = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) {
                try {
                    Lucky.getApplication().getDaoSession().getLateContactBeanDao().deleteAll();
                    Lucky.getApplication().getDaoSession().getGroupListbeanDao().deleteAll();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        observable.subscribeOn(Schedulers.io()).subscribe();
    }
}
